﻿//-----------------------------------------------------------------------
// <copyright file="MLNewNamespace.cs" company="MAPILab Ltd">
//     Copyright (c) MAPILab Ltd.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MAPILab.SharePoint.Explorer.ObjectBrowser
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>
    /// Form for adding new namespace to default namespaces in prperties form
    /// </summary>
    public partial class MLNewNamespace : Form
    {
        #region Field

        /// <summary>
        /// New Namespace full path
        /// </summary>
        private string newUsingLine = String.Empty;

        #endregion

        #region Property

        /// <summary>
        /// Get new namespace full path
        /// </summary>
        public string NewUsingLine
        {
            get { return newUsingLine; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Form constructor
        /// </summary>
        public MLNewNamespace()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        /// <summary>
        /// On preview key down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NamespaceBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            TextBoxIsChanged();
        }

        /// <summary>
        /// on NamespaceBox click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NamespaceBox_Click(object sender, EventArgs e)
        {
            TextBoxIsChanged();
        }

        /// <summary>
        /// OK button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Click(object sender, EventArgs e)
        {
            DialogResult = GetDialogResult();
            Close();
        }

        /// <summary>
        /// Cancel button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        #endregion

        #region Method

        /// <summary>
        /// When thext box is changed for the first time
        /// </summary>
        private void TextBoxIsChanged()
        {
            NamespaceBox.Text = "";
            NamespaceBox.ForeColor = Color.Black;
            NamespaceBox.Click -= new EventHandler(NamespaceBox_Click);
            NamespaceBox.PreviewKeyDown -= new PreviewKeyDownEventHandler(NamespaceBox_PreviewKeyDown);
        }

        /// <summary>
        /// Generate dialog result for this form
        /// </summary>
        /// <returns></returns>
        private DialogResult GetDialogResult()
        {
            if (NamespaceBox.Text.Contains(" "))
            {
                NamespaceBox.Text = System.Text.RegularExpressions.Regex.Replace(NamespaceBox.Text, "[\n ]", "");
            }

            if (NamespaceBox.Text != "Typehere" && NamespaceBox.Text != null && NamespaceBox.Text != String.Empty)
            {
                newUsingLine = NamespaceBox.Text;
                return DialogResult.OK;
            }
            else
            {
                return DialogResult.No;
            }
        }

        #endregion
    }
}
